define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {

        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wddj/operationt/city/index' + location.search,
                    add_url: 'wddj/operationt/city/add',
                    edit_url: 'wddj/operationt/city/edit',
                    del_url: 'wddj/operationt/city/del',
                    multi_url: 'wddj/operationt/city/multi',
                    import_url: 'wddj/operationt/city/import',
                    cost_allocation_url: 'wddj/operationt/cost_allocation/index',
                    commission_ratio_url: 'wddj/operationt/commission_ratio/index',
                    table: 'wddj_city_trailer',
                }
            });
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'city', title: __('City'), operate: 'LIKE', table: table, class: 'autocontent'},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Status normal'),"hidden":__('Status hidden')}, formatter: Table.api.formatter.status},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:[
                                {
                                    name: 'commission_ratio',
                                    text: '抽成比例',
                                    title: '抽成比例',
                                    classname: 'btn btn-xs btn-primary btn-dialog bg-olive',
                                    extend: 'data-area=["100%","100%"]',
                                    url: 'wddj/operationt/commission_ratio/index?city_id={id}',
                                },
                                {
                                    name: 'cost_allocation',
                                    text: '费用配置',
                                    title: '费用配置',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    extend: 'data-area=["100%","100%"]',
                                    url: 'wddj/operationt/cost_allocation/index?city_id={id}',
                                },
                            ]}
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        cost_allocation: function () {
            // 初始化表格参数配置
            Table.api.init();
            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.length > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
            Controller.api.bindevent();
        },
        table: {
            starting_price: function () {
                // 会议日程
                var table_starting_price = $("#table_starting_price");
                table_starting_price.bootstrapTable({
                    url: 'wddj/operationt/starting_price/index?city_id=' + Config.city_id,
                    extend: {
                        index_url: 'wddj/operationt/starting_price/index' + location.search,
                        add_url: 'wddj/operationt/starting_price/add?city_id='+ Config.city_id,
                        edit_url: 'wddj/operationt/starting_price/edit',
                        del_url: 'wddj/operationt/starting_price/del',
                        multi_url: 'wddj/operationt/starting_price/multi',
                        import_url: 'wddj/operationt/starting_price/import',
                        table: 'wddj_starting_price',
                    },
                    toolbar: '#toolbar_starting_price',
                    sortName: 'weigh',
                    search: false,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id')},
                            {field: 'start_time', title: __('Start_time')},
                            {field: 'end_time', title: __('End_time')},
                            {field: 'price', title: __('Price'), operate:'BETWEEN'},
                            {field: 'kilometer', title: __('Kilometer'), operate:'BETWEEN'},
                            {field: 'weigh', title: __('Weigh'), operate: false},
                            {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                            {field: 'operate', title: __('Operate'), table: table_starting_price, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ]
                });

                // 为会议日程绑定事件
                Table.api.bindevent(table_starting_price);
            },
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
